package com.example.io_demo.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author zhaoyunlong
 * @date 2020/10/29
 * @description 在java中 需要读写文件中的数据，需要使用流的概念
 * 流表示将数据从一个文件传送到另一个文件，包含一个流向的问题
 * 最终需要选择一个参照物，以当前程序作为参照物
 * 从一个文件中读取数据到程序  叫做输入流
 * 从程序中输出数据到另外一个文件，叫输出流
 */
public class InputIODemo {

    public static void main(String[] args) {
//        readFileOne();
//        readFileTwo();
//        readFileThree();
        writerFile();
//        copyFile();
    }

    /**
     * inputStream.read() 类似与迭代器的next() 方法，每次单一的读取一个字符，效率较低
     */
    private static void readFileOne() {
        try (InputStream inputStream = new FileInputStream("file-demo.txt")) {
            int read;
            while ((read = inputStream.read()) != -1) {
                System.out.println(read + ":  " + (char) read);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * inputStream.read() 类似与迭代器的next() 方法，
     *  不过可以给定缓冲去，一次读取给定的缓冲区长度的字符串，
     */
    private static void readFileTwo() {
        try (InputStream inputStream = new FileInputStream("file-demo.txt")) {
            // 给出读取数据的缓冲区
            byte[] buffer = new byte[1024];
            int readData;
            while ((readData = inputStream.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, readData));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *  因为inputStream.read() 方法类似与迭代器的next()方法，所以一个文件流不可以重复的循环获取数据
     */
    private static void readFileThree() {
        try (InputStream inputStream = new FileInputStream("file-demo.txt")) {
            byte[] buffer = new byte[1024];
            int readData;
            while ((readData = inputStream.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, readData));
            }

            // 流中的数据已经在上个循环中轮询完（已经指向数据最后），所以在此读取流的数据，已经读取不到
            // 无数据输出
            int read;
            while ((read = inputStream.read()) != -1) {
                System.out.println(read + ":  " + (char) read);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void writerFile() {
        try( OutputStream outputStream = new FileOutputStream("file-copy.txt")) {
            outputStream.write(12);
            outputStream.write("dklsjoehtequtoueqtoge".getBytes());
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void copyFile() {
        try (InputStream inputStream = new FileInputStream("file-demo.txt"); OutputStream outputStream = new FileOutputStream("file-copy.txt")) {
            byte[] buffer = new byte[1024];
            int readData;
            while ((readData = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, readData);
            }
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
